package todo.core.bso;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import todo.core.bso.TodoService;
import todo.core.domain.Todo;
import todo.core.testdb.DerbyManager;



public class TodoServiceTest {
	private static XmlBeanFactory factory = null;
	private static String id = null;
	private static TodoService service = null;

	@BeforeClass
	public static void runUpBeforeClass() throws Exception {
		BasicConfigurator.configure();
		Logger.getRootLogger().setLevel(Level.INFO);
		
		DerbyManager.createDatabase();
		
		factory = new XmlBeanFactory(new ClassPathResource("example-applicationContext.xml") );
		service = (TodoService) factory.getBean("todoService");
	}
	
	@AfterClass
	public static void runAfterClass() throws Exception {
		DerbyManager.cleanupDatabase();
	}

	@Test
	public void testSave() {
		Todo aTodo = new Todo();
		aTodo.setTitle("jtest");
		aTodo.setDescription("description");
		aTodo.setComplete(false);
		
		service.save(aTodo);
		
		id = aTodo.getTodoId();
	}

	@Test
	public void testFindById() {
		Todo aTodo = service.findById(id);
		assertEquals(aTodo.getTitle(), "jtest");
		assertNotNull(aTodo);
	}
	
	@Test
	public void testDelete() {
		Todo aTodo = service.findById(id);

		service.delete(aTodo);
	}

}
